package com.liangtian.mdm.dm.jsp.common;

import com.liangtian.mdm.dm.common.constant.TableConst;
import com.liangtian.mdm.dm.common.constant.UrlConst;
import com.liangtian.mdm.dm.common.enums.Type;
import org.apache.commons.lang3.StringUtils;
import com.liangtian.mdm.dm.config.model.AttrConfig;
import com.liangtian.mdm.dm.group.model.AttrGroup;
import com.liangtian.mdm.dm.model.model.Model;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class EditJsCreate {


    /**
     * * <p>Title: 替换查询出来的特殊字符 ：“@”</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:15:04
     */
    public static String ReplaceVar(String flag) {
        StringBuilder sb = new StringBuilder();
        String[] s = flag.split("@");
        for (int i = 0; i < s.length; i++) {
            sb.append("var " + s[i] + "=0; \r\n");
        }
        return sb.toString();
    }

    /**
     * * <p>Title: 替换修改js文件</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:16:44
     */
    public static String ReplaceAll(String operType, String readFilePath, String script, List<AttrConfig> list, Model model, List<AttrConfig> selectCascadList) {
        String tableName = model.getTableName();
        String res = ReplaceDataLists(model, list, selectCascadList);
        script = script.replace("{ngApp}", tableName + operType + "App");
        script = script.replace("{ngControl}", tableName + operType + "Control");
        if ("updateEdit".equals(operType) || "treeTabUpdateEdit".equals(operType)) {
            script = script.replace("{tableName}", tableName + TableConst.MODIFYSUFFIX); //+"_update");
            script = script.replace("{UpdateEditColumn}", JspUtil.spellColumn(model, Type.UPDATEEDIT));
        } else {
            script = script.replace("{tableName}", tableName);
        }
        script = script.replace("{formId}", tableName).replace("{formName}", tableName)
                .replace("{EditColumn}", JspUtil.spellColumn(model, Type.EDIT))
                .replace("{selectControl}", UrlConst.ONEURL)
                .replace("{UpdateControl}", UrlConst.UPDATEURL)
                .replace("{DataList}", res).replace("@R#", "\r\n");
        return script;
    }

    /**
     * * <p>Title: 动态替换生成的日期js语句</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:17:30
     */
    public static String ReplaceDataLists(Model model, List<AttrConfig> attrList, List<AttrConfig> selectCascadList) {

        StringBuilder sb = new StringBuilder();
        if (attrList != null) {
            for (int i = 0; i < attrList.size(); i++) {
                String column = attrList.get(i).getAttrColumn().toLowerCase();
                sb.append("     $scope." + column + "=data." + column + ";   $RN$");
                if ("popList".equals(attrList.get(i).getAttrType())) {
                    sb.append("     PopValueFun" + column + "($scope." + column + ");   $RN$");
                } else if ("listTree".equals(attrList.get(i).getAttrType())) {
                    if (1 == attrList.get(i).getIsDefaultParentType()) {//说明是父子树
                        sb.append("     $scope.pid=data.pid;   $RN$");
                        sb.append("     TreeValueFun" + column + "($scope." + column + ");   $RN$");
                    } else {//说明不是父子树
                        sb.append("     TreeValueFun" + column + "($scope." + column + ");   $RN$");
                    }
                } else if ("checkBoxType".equals(attrList.get(i).getAttrType())) {
                    sb.append("     checkBoxValueFun" + column + "($scope." + column + ");   $RN$");
                } else if ("selectList".equals(attrList.get(i).getAttrType())) {//如果是下拉框
                    if ("2".equals(attrList.get(i).getModuleType())) {//如果是级联的话
                        String attrId = attrList.get(i).getCascadedAttr();//获取级联列的attr_Id
                        for (int j = 0; j < selectCascadList.size(); j++) {//所有级联对象
                            if (attrId.equals(selectCascadList.get(j).getAttrId())) {
                                AttrConfig preAttr = selectCascadList.get(j);
                                sb.append("     SelectValueFun" + column + "(data." + column + ",data." + preAttr.getAttrColumn().toLowerCase() + ");   $RN$");
                            }
                        }

                    }

                } else if ("BLOB".equals(attrList.get(i).getAttrType())) {//如果是文件类型
                    sb.append("      var " + column + "files = data." + column + "; $RN$")
                            .append("      if(undefined != " + column + "files){ $RN$")
                            .append("         if(" + column + "files.length==1){ $RN$")
                            .append("              document.getElementById('fileName'+'" + column + "').innerHTML=" + column + "files[0].fileName; $RN$")
                            .append("         }else{ $RN$")
                            .append("              document.getElementById('fileName'+'" + column + "').innerHTML=" + column + "files[0].fileName+" + "\"等共\"" + "+" + column + "files.length+" + "\"个附件\"" + " $RN$")
                            .append("         } $RN$")
                            .append("      }  $RN$");

                }
            }
        }
        return sb.toString().replace("$RN$", "\r\n");
    }

    /**
     * * <p>Title: 动态生成级联下拉框的语句</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:19:20
     */
    public static String spellSelectValue(Model model, List<String> selectList) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < selectList.size(); i++) {
            sb.append("  function SelectValueFun" + selectList.get(i).toLowerCase() + "(a,b){ $RN$");
            sb.append("      $http({ $RN$");
            sb.append("           url : basePath+'/genJsp/getSelectData', $RN$");
            sb.append("          params: { $RN$");
            sb.append("             'modelId' : '" + model.getId() + "','attrColumn':'" + selectList.get(i) + "', $RN$");
            sb.append("             columnValue:b, isCascad :'yes' $RN$");
            sb.append("          }, $RN$");
            sb.append("          method:'POST' $RN$");
            sb.append("        }).success(function(data,header,config,status){ $RN$");
            sb.append("            $scope." + selectList.get(i).toLowerCase() + "s= data.data; $RN$");
            sb.append("	        }).error(function(data,header,config,status){ $RN$");
            //sb.append("               alert('error'); $RN$");
            sb.append("	        }); $RN$");
            sb.append("   } $RN$");
        }
        return sb.toString().replace("$RN$", "\r\n");
    }


    /**
     * * <p>Title: 动态生成pop弹出框的语句</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:19:20
     */
    public static String spellPopValue(Model model, List<String> popList) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < popList.size(); i++) {
            sb.append("  function PopValueFun" + popList.get(i).toLowerCase() + "(val){ $RN$");
            sb.append("      $http({ $RN$");
            sb.append("          url:basePath+'/genJsp/getColum', $RN$");
            sb.append("          params: { $RN$");
            sb.append("             'modelId' : '" + model.getId() + "','attrColumn':'" + popList.get(i) + "', $RN$");
            sb.append("             'Id':val $RN$");
            sb.append("          }, $RN$");
            sb.append("          method:'POST' $RN$");
            sb.append("        }).success(function(data,header,config,status){ $RN$");
            sb.append("            if(data.data.length>0){ $RN$");
            sb.append("                $('#" + popList.get(i).toLowerCase() + "Name').val(data.data[0].name); $RN$");
            sb.append("                $('#" + popList.get(i).toLowerCase() + "').val(data.data[0].value); $RN$");
            sb.append("         } $RN$");
            sb.append("         else $RN$");
            sb.append("            $('#" + popList.get(i).toLowerCase() + "Name').val(''); $RN$");
            sb.append("	        }).error(function(data,header,config,status){ $RN$");
            //sb.append("               alert('error'); $RN$");
            sb.append("	        }); $RN$");
            sb.append("   } $RN$");
        }
        return sb.toString().replace("$RN$", "\r\n");
    }

    public static String spellTreeValue(Model model, List<AttrConfig> columList) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < columList.size(); i++) {
            String colum = columList.get(i).getAttrColumn();
            if (columList.get(i).getIsDefaultParentType() == 1) {//如果是父子属性
                sb.append("  function TreeValueFun" + colum.toLowerCase() + "(int){ $RN$");
                sb.append("       $http({ $RN$");
                sb.append("           url:basePath+'/genJsp/getColum', $RN$");
                sb.append("           params: { $RN$");
                sb.append("                'modelId' : '" + model.getId() + "','attrColumn':'" + colum + "', $RN$");
                sb.append("                'Id':$scope.pid $RN$");
                sb.append("          }, $RN$");
                sb.append("           method:'POST' $RN$");
                sb.append("       }).success(function(data,header,config,status){ $RN$");
                sb.append("           if(data.data.length>0){ $RN$");
                sb.append("                    $('#" + colum.toLowerCase() + "').val(data.data[0].value); $RN$");
                sb.append("                    $('#" + colum.toLowerCase() + "Name').val(data.data[0].name); $RN$");
                sb.append("                   $scope.pid = data.data[0].value; $RN$");
                sb.append("                   $scope." + colum.toLowerCase() + " = data.data[0].value; $RN$");
                //sb.append("               $('#" + colum.toLowerCase() + "').val(data.data[0].name); $RN$");
                //sb.append("               $('#pid').val(data.data[0].value); $RN$");
                sb.append("           } $RN$");
                sb.append("           else{ $RN$");
                //sb.append("               $('#" + colum.toLowerCase() + "').val('')}; $RN$");
                sb.append("                   $scope.pid = 0; $RN$");
                sb.append("                   $scope." + colum.toLowerCase() + " = 0; $RN$");
                sb.append("                   $('#" + colum.toLowerCase() + "').val('0');$RN$");
                sb.append("                   $('#" + colum.toLowerCase() + "Name').val('" + columList.get(i).getViewName() + "');} $RN$");
                sb.append("	      }).error(function(data,header,config,status){ $RN$");
                //	sb.append("               alert('error'); $RN$");
                sb.append("	      }); $RN$");
                sb.append("      } $RN$");
            } else {
                sb.append("  function TreeValueFun" + colum.toLowerCase() + "(int){ $RN$");
                sb.append("         $http({ $RN$");
                sb.append("              url:basePath+'/genJsp/getColum', $RN$");
                sb.append("              params: { $RN$");
                sb.append("                  'modelId' : '" + model.getId() + "','attrColumn':'" + colum + "', $RN$");
                sb.append("                  'Id':int $RN$");
                sb.append("                }, $RN$");
                sb.append("              method:'POST' $RN$");
                sb.append("            }).success(function(data,header,config,status){ $RN$");
                sb.append("                if(data.data.length>0){ $RN$");
                sb.append("                    $('#" + colum.toLowerCase() + "').val(data.data[0].value); $RN$");
                sb.append("                    $('#" + colum.toLowerCase() + "Name').val(data.data[0].name); $RN$");
                sb.append("                    $scope." + colum.toLowerCase() + " = data.data[0].value; $RN$");
                sb.append("                } $RN$");
                sb.append("                else{ $RN$");
                sb.append("                    $scope." + colum.toLowerCase() + " = 0; $RN$");
                sb.append("                    $('#" + colum.toLowerCase() + "').val('0');$RN$");
                sb.append("                    $('#" + colum.toLowerCase() + "Name').val('" + columList.get(i).getViewName() + "');} $RN$");
                sb.append("	           }).error(function(data,header,config,status){ $RN$");
                //sb.append("                  alert('error'); $RN$");
                sb.append("	           }); $RN$");
                sb.append("    } $RN$");
            }
        }
        return sb.toString().replace("$RN$", "\r\n");
    }

    /**
     * * <p>Title: 动态生成复选框的js语句</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:20:18
     */
    public static String spellCheckboxValue(String script, List<String> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append("  function checkBoxValueFun" + list.get(i) + "(conds){ $RN$");
            sb.append("       if(conds!=null && conds!=''){ $RN$");
            sb.append("           var strs=$scope." + list.get(i) + "s; $RN$");
            sb.append("           var str=conds.split(','); $RN$");
            sb.append("           var checks = document.getElementsByName('" + list.get(i) + "'); $RN$");
            sb.append("              for(var i=0;i<strs.length;i++){ $RN$");
            sb.append("                 for(var j=0;j<str.length;j++){ $RN$");
            sb.append("                      if(str[j]==strs[i].value){ $RN$");
            sb.append("                         checks[i].checked=true; $RN$");
            sb.append("                      } $RN$");
            sb.append("                 } $RN$");
            sb.append("              } $RN$");
            sb.append("        } $RN$");
            sb.append("   }; $RN$");
        }
        return sb.toString().replace("$RN$", "\r\n");

    }
	
	/*public static String spellFileFun(List<String> fileList, Model model,String operType) {
		StringBuilder sb = new StringBuilder();
		if("updateEdit".equals(operType) || "treeTabUpdateEdit".equals(operType)){
			for (int i = 0; i < fileList.size(); i++) {
				//获取文件列名
				String fileColum = fileList.get(i).toLowerCase();
				sb.append("  $http({   $RN$");
				sb.append("       url:basePath+'/file/queryFiles',   $RN$");
				sb.append("       params: {  $RN$");
				sb.append("         'surveyType' : '"+model.getId()+"','dataId':$scope.updateId,'fileColumn':'"+fileColum.toUpperCase()+"'   $RN$");
				sb.append("        },  $RN$");
				sb.append("        method:'POST'  $RN$");
				sb.append("    }).success(function(data,header,config,status){  $RN$");
				sb.append("        if(data.length>0){  $RN$");
				sb.append("            document.getElementById('fileName"+fileColum+"').innerHTML='<font size=\"4px\" style=\"font-weight:bold;\">'+"+"'附件名称:'+"+"'</font>'+"+"data[0].fileName;    $RN$");
				sb.append("        }else{  $RN$");
				sb.append("            document.getElementById('fileNamec_003').innerHTML='未上传附件!'; $RN$");
				sb.append("        } $RN$");
				sb.append("    }).error(function(data,header,config,status){   $RN$");
				sb.append("        toastr.error('-----查询附件失败------!');   $RN$");
				sb.append(" });    $RN$");
				sb.append("$RN$");
			}
		}else{
			for (int i = 0; i < fileList.size(); i++) {
				//获取文件列名
				String fileColum = fileList.get(i).toLowerCase();
				sb.append("  $http({   $RN$");
				sb.append("     url:basePath+'/file/queryFiles',   $RN$");
				sb.append("     params: {  $RN$");
				sb.append("         'surveyType' : '"+model.getId()+"','dataId':$scope.id,'fileColumn':'"+fileColum.toUpperCase()+"'   $RN$");
				sb.append("        },  $RN$");
				sb.append("     method:'POST'  $RN$");
				sb.append("    }).success(function(data,header,config,status){  $RN$");
				sb.append("       if(data.length>0){  $RN$");
				sb.append("          document.getElementById('fileName"+fileColum+"').innerHTML=data[0].fileName;  $RN$");
				sb.append("       }  $RN$");
				sb.append("    }).error(function(data,header,config,status){   $RN$");
				sb.append("        toastr.error('-----查询附件失败------!');   $RN$");
				sb.append("    });    $RN$");
				sb.append("$RN$");
			}
		}
		return sb.toString().replace("$RN$", "\r\n");

	}*/

    /**
     * * <p>Title: 生成替换js模板</p>
     *
     * @author : xiaoliu
     * @date :2016年8月8日下午2:21:25
     */
    @SuppressWarnings({"unchecked"})
    public static void creatEditJs(Map<String, Object> res_Map, String readFilePath, Model model, Map<String, Object> resMap,
                                   String operType, String backPage, Map<String, Object> result_map, boolean bool, List<AttrGroup> groupLst) {
        String script = "";
        try {
            script = JspUtil.getTmpJspContent(readFilePath); // 获取js页面内容
            if ("0".equals(model.getClassType()) && !StringUtils.isBlank(model.getMaintableId())) {
                script = script.replace("{mainIdValue}", spellMainId());
                script = script.replace("{returnUrl}", getEditReturnCongPath(model, backPage));
            } else {
                script = script.replace("{mainIdValue}", "");
                script = script.replace("{returnUrl}", getEditReturnMainPath(model, backPage));
            }

            List<String> ngshowcolumList = (List<String>) res_Map.get("columList");
            String fun = (String) res_Map.get("sb");
            List<AttrConfig> attributeRadioList = (List<AttrConfig>) resMap.get("attributeRadioList");//所有单选按钮对象
            List<AttrConfig> attributeCheckboxList = (List<AttrConfig>) resMap.get("attributeCheckboxList");//所有复选框对象
            List<AttrConfig> selectCascadList = (List<AttrConfig>) resMap.get("selectCascadList");//所有级联下拉框单选对象
            if (ngshowcolumList.size() == 0 && ngshowcolumList.isEmpty()) {
//		    	 if("0".equals(model.getModelType())){//表示主数据
//		    		 script = script.replace("{checkNgShow}", "var flag = checkAddForum(); if(flag){  submitData();  }"); 
//		    	 }else{//表示字典
                script = script.replace("{checkNgShow}", "submitData();");
//		    	 }

            } else {
                //script=script.replace("{checkNgShow}", JspUtil.spellNgShow(model,ngshowcolumList,attributeRadioList,attributeCheckboxList));

                script = script.replace("{checkNgShow}", JspUtil.spellNgShow(ngshowcolumList, attributeRadioList, attributeCheckboxList));
            }
            if (null != fun && !"".equals(fun)) {
                script = script.replace("{ngShowFunction}", fun);
            } else {
                script = script.replace("{ngShowFunction}", "");
            }

            List<AttrConfig> configLst = (List<AttrConfig>) resMap.get("jspAttrLst");
            script = script.replace("{dataViewEditSource}", AddJsCreate.dataViewEditSource(model, operType, groupLst));
            script = script.replace("{getIsMoreViewApply}", AddJsCreate.getIsMoreViewApply(model, groupLst));
            script = script.replace("{getIsMoreClearAttrs}", AddJsCreate.getIsMoreClearAttrs(model, groupLst, configLst));
            script = script.replace("{checkColumnsAdd}", AddJsCreate.checkColumnsAdd(model, configLst, groupLst, operType));
            //判断是否含有唯一校验
            // script = script.replace("{checkColumnIsShow}",AddJsCreate.checkColumnIsShow(configLst));
            //唯一性校验方法
            // script = script.replace("{checkWeiYiColumn}",AddJsCreate.checkWeiYiColumn(configLst,model,operType));
            // script = script.replace("{spellWeiYiMethod}",AddJsCreate.spellWeiYiMethod(configLst));
            Map<String, Object> jsMap = AddJsCreate.spellJsAddStr(operType, model, configLst);
            if (null != jsMap && !"".equals(jsMap)) {
                script = script.replace("{selectList}", (String) jsMap.get("sb"));
            } else {
                script = script.replace("{selectList}", "");
            }
            boolean selectFlag = (boolean) jsMap.get("selectFlag");
            if (selectFlag) {
                Map<String, Map<String, AttrConfig>> selectMap = (Map<String, Map<String, AttrConfig>>) resMap.get("selectMap");
                script = script.replace("{selectClickFunction}", JspUtil.spellSelectClick(operType, model, selectMap));
            } else {
                script = script.replace("{selectClickFunction}", "");
            }
            boolean rcFlag = (boolean) resMap.get("flag");
            if (rcFlag)
                script = script.replace("{checkRadioOrCheckboxIsNull}", JspUtil.createCheckFunction(operType, attributeRadioList, attributeCheckboxList));
            else {
                script = script.replace("{checkRadioOrCheckboxIsNull}", "");
            }

            // 拼接checkbox的值回填方法以及取值方法
            List<String> checkboxLst = (List<String>) jsMap.get("checkboxList");
            if (null != checkboxLst && !checkboxLst.isEmpty()) {
                script = script.replace("{checkBoxValueFunction}",
                        spellCheckboxValue(script, checkboxLst));
                script = script.replace("{checkboxValues}", spellCheckboxSubmitValue(checkboxLst));
            } else {
                script = script.replace("{checkBoxValueFunction}", "");
                script = script.replace("{checkboxValues}", "");
            }
            //pop弹出框
            List<String> popList = (List<String>) jsMap.get("popList");
            if (null != popList) {//&& !columList.isEmpty()
                script = script.replace("{popValues}",
                        (String) AddJsCreate.createAddPOPParams(model, configLst).get("sb"));
                script = script.replace("{PopValueFunction}",
                        spellPopValue(model, popList));
            } else {
                script = script.replace("{popValues}", "");
                script = script.replace("{PopValueFunction}", "");
            }
            //级联下拉框
            List<String> selectList = (List<String>) jsMap.get("selectList");
            if (null != selectList) {//&& !columList.isEmpty()
                //根据selectList 的级联列，获取级联列的关联列

                script = script.replace("{SelectValueFunction}", spellSelectValue(model, selectList));
            } else {
                script = script.replace("{SelectValueFunction}", "");
            }
            //树列表
            List<AttrConfig> treeFlagList = (List<AttrConfig>) jsMap.get("treeFlagList");
            if (null != treeFlagList) {// && !columList.isEmpty()
                script = script.replace("{treeValues}",
                        (String) AddJsCreate.createAddTreeParams(model, configLst).get("sb"));
                script = script.replace("{TreeValueFunction}",
                        spellTreeValue(model, treeFlagList));
            } else {
                script = script.replace("{treeValues}", "");
                script = script.replace("{TreeValueFunction}", "");
            }
            //获取所有file数据的
            List<String> fileList = (List<String>) jsMap.get("fileFlag");
            boolean isFile = false;
            if (fileList.size() > 0) {
                isFile = true;
                /*				script = script.replace("{spellFileFun}", spellFileFun(fileList,model,operType));
                 */
            }
//			else{
//				script = script.replace("{spellFileFun}", "");
//			}

            // 自定义编码
            String autoCode = (String) res_Map.get("autoCode");
            if (null != autoCode && !"".equals(autoCode)) {
                script = script.replace("{autoCodeFunction}", autoCode);
            } else {
                script = script.replace("{autoCodeFunction}", "");
            }
            script = script.replace("{callAutoCodeFunction}", CodeBlock.callAutoCodeFunction(configLst));

            Map<String, Object> map = AddJsCreate.createAddParams(null, model, operType, configLst, groupLst);
            script = script.replace("{ParamList}", (String) map.get("sb"));
            script = script.replace("{successShow}", AddJsCreate.spellEditReturnResult(isFile, model, backPage, operType, result_map).get("sb"));
            List<AttrConfig> list = (List<AttrConfig>) map.get("attrConfigList");
            List<AttrConfig> fileLst = (List<AttrConfig>) resMap.get("fileLst");
            if (fileLst.isEmpty()) {
                script = script.replace("{spellFileFun}", "");
            } else {
                script = script.replace("{spellFileFun}", AddJsCreate.spellAddFiles(fileLst));
            }
            script = EditJsCreate.ReplaceAll(operType, readFilePath, script, list, model, selectCascadList);
            JspUtil.writeInJsp(script, readFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String spellMainId() {
        StringBuilder sb = new StringBuilder();
        sb.append("var mainId = angular.element($('#mainId')).val(); \r\n");
        sb.append("   $scope.mainid=mainId; \r\n");
        return sb.toString();
    }

    private static String spellOriginId() {
        StringBuilder sb = new StringBuilder();
        sb.append("var originId = angular.element($('#originId')).val(); \r\n");
        sb.append("   $scope.originId=originId; \r\n");
        return sb.toString();
    }

    private static String spellCheckboxSubmitValue(List<String> checkboxLst) {
        StringBuilder sb = new StringBuilder();
        for (String string : checkboxLst) {
            sb.append("       var flag" + string + "=0; $RN$")
                    .append("       var array" + string + "=[]; $RN$")
                    .append("       var check" + string + "s=document.getElementsByName('" + string + "'); $RN$")
                    .append("       for(var i=0;i<check" + string + "s.length;i++){ $RN$")
                    .append("          if(check" + string + "s[i].checked) $RN$")
                    .append("              array" + string + ".push(check" + string + "s[i].value) $RN$")
                    .append("       }  $RN$")
                    .append("       if(array" + string + ".length!=1) $RN$")
                    .append("           flag" + string + "=1; $RN$").append("$RN$");
        }
        return sb.toString().replace("$RN$", "\r\n");
    }

    /**
     * <p>Description: 在编辑页面，点击返回按钮时触发的方法 (主)</p>
     * <p>@param model---本模型对象
     * <p>@param backPage--返回页面参数
     *
     * @author : xiaoliu
     * @date :2016年11月29日下午3:55:16
     */
    public static String getEditReturnMainPath(Model model, String backPage) {
        StringBuilder sb = new StringBuilder();
        if ("apply".equals(backPage)) {//如果是从列表模板申请过来的
            sb.append("  if(returnBase=='apply'){  $RN$")
                    .append("     location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_" + backPage + ".jsp';  $RN$")
                    .append("  }else{  $RN$")
                    .append("     location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_wh.jsp';  $RN$")
                    .append("  }  $RN$");
        } else if ("treeTabApply".equals(backPage)) {//如果是树模板申请页面过来的
            sb.append("   if(returnBase=='apply'){  $RN$")
                    .append("       location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_" + backPage + ".jsp?" + JspUtil.spellColumn(model, Type.MAIN) + "='+" + JspUtil.spellColumn(model, Type.EDIT) + ";  $RN$")
                    .append("   }else{  $RN$")
                    .append("       location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_treeTabWh.jsp?" + JspUtil.spellColumn(model, Type.MAIN) + "='+" + JspUtil.spellColumn(model, Type.EDIT) + ";  $RN$")
                    .append("   }  $RN$");
        } else {
            if ("treeTabUpdate".equals(backPage)) {
                sb.append("      location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_" + backPage + ".jsp?" + JspUtil.spellColumn(model, Type.MAIN) + "='+" + JspUtil.spellColumn(model, Type.UPDATEEDIT) + ";  $RN$");
            } else {
                sb.append("      location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_" + backPage + ".jsp';  $RN$");
            }

        }
        return sb.toString().replace("$RN$", "\r\n");
    }

    /**
     * <p>Description: 在编辑页面，点击返回按钮时触发的方法 (从)</p>
     * <p>@param model---本模型对象
     * <p>@param backPage--返回页面参数
     *
     * @author : xiaoliu
     * @date :2016年11月29日下午3:55:16
     */
    public static String getEditReturnCongPath(Model model, String backPage) {
        StringBuilder sb = new StringBuilder();
        if ("apply".equals(backPage)) {//如果是从列表模板申请过来的
            sb.append("  if(returnBase=='apply'){  $RN$")
                    .append("       location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_" + backPage + ".jsp?mainId='+$scope.mainid;  $RN$")
                    .append("  }else{  $RN$")
                    .append("       location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_wh.jsp?mainId='+$scope.mainid;  $RN$")
                    .append("  }  $RN$");
        } else if ("treeTabApply".equals(backPage)) {//如果是树模板申请页面过来的
            sb.append("  if(returnBase=='apply'){  $RN$")
                    .append("       location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_" + backPage + ".jsp?mainId='+$scope.mainid;  $RN$")
                    .append("  }else{  $RN$")
                    .append("       location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_treeTabWh.jsp?mainId='+$scope.mainid;  $RN$")
                    .append("   }  $RN$");
        } else {
            sb.append("     location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_" + backPage + ".jsp?mainId='+$scope.mainid;  $RN$");
        }
        return sb.toString().replace("$RN$", "\r\n");
    }

    /**
     * <p>Description: 在编辑页面，点击返回按钮时触发的方法 (从接收主传递参数)</p>
     * <p>@param model---本模型对象
     * <p>@param backPage--返回页面参数
     *
     * @author : xiaoliu
     * @date :2016年11月29日下午3:55:16
     */
    public static String getEditReturnPath(Model model, String backPage) {
        StringBuilder sb = new StringBuilder();
        if ("apply".equals(backPage)) {//如果是从列表模板申请过来的
            sb.append("   if(returnBase=='apply'){  $RN$")
                    .append("        location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_" + backPage + ".jsp?mainId='+$('#mainId').val();  $RN$")
                    .append("   }else{  $RN$")
                    .append("        location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_wh.jsp?mainId='+$('#mainId').val();  $RN$")
                    .append("   }  $RN$");
        } else if ("treeTabApply".equals(backPage)) {//如果是树模板申请页面过来的
            sb.append("    if(returnBase=='apply'){  $RN$")
                    .append("        location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_" + backPage + ".jsp?mainId='+$('#mainId').val();  $RN$")
                    .append("    }else{  $RN$")
                    .append("        location.href=basePath+'/am/" + model.getTableName() + "/" + model.getTableName() + "_treeTabWh.jsp?mainId='+$('#mainId').val();  $RN$")
                    .append("    }  $RN$");
        }
        return sb.toString().replace("$RN$", "\r\n");
    }

}
